package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserHouse;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserHouseDaoImpl extends TempDataObjectDao<UserHouse> {

    public UserHouse getUserHouse(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_house where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserHouse data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserHouse data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_house(userId,lastClickTime,totalClickTimes) values (?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setLong(i++, data.getLastClickTime());
            call.setLong(i++, data.getTotalClickTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserHouse data) {
        PreparedStatement call = null;
        String sql = "update t_u_house set lastClickTime=?,totalClickTimes=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getLastClickTime());
            call.setLong(i++, data.getTotalClickTimes());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserHouse parseData(ResultSet rs) throws SQLException {
        UserHouse data = new UserHouse();
        data.setUserId(rs.getLong("userId"));
        data.setLastClickTime(rs.getLong("lastClickTime"));
        data.setTotalClickTimes(rs.getLong("totalClickTimes"));
        return data;
    }
}
